#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Time          : 2025/4/21 14:46
# File          : compress_image
# @Author       : MingTai
# @Department   : csc_qa
from PIL import Image


def compress_image(input_path, output_path, quality=80):
    try:
        # 打开图片
        with Image.open(input_path) as img:
            # 检查图片模式，如果是 RGBA 则转换为 RGB
            if img.mode == 'RGBA':
                img = img.convert('RGB')
            # 保存压缩后的图片
            img.save(output_path, optimize=True, quality=quality)
        print(f"图片已压缩并保存至 {output_path}")
    except FileNotFoundError:
        print(f"错误: 文件 {input_path} 未找到。")
    except Exception as e:
        print(f"错误: 发生了未知错误 {e}")


if __name__ == "__main__":
    input_image_path = "/Users/tianmingtai/Downloads/ready/111.png"  # 替换为你的输入图片路径
    output_image_path = "/Users/tianmingtai/Downloads/ready/1112.jpg"  # 替换为你的输出图片路径
    compress_image(input_image_path, output_image_path)
